package echo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class EchoApplication {
    public static void main(String[] args) throws IOException {
        // 启动服务端
        EchoServer server = new EchoServer(12345);
        server.startService();

        // 启动客户端
        EchoClient client = new EchoClient(12345);
        client.startService();
    }
}
class EchoClient {
    // TODO
    private Integer port;

    public EchoClient(Integer port) {
        this.port = port;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public void startService() throws IOException {
        Scanner scanner = new Scanner(System.in);

        //客户端通过服务端IP+端口连接服务器
        Socket socket = new Socket("127.0.0.1",port);

        //启动子线程专门接收服务器数据
        new Thread(){
            @Override
            public void run() {

                DataInputStream dataInputStream = null;
                try {
                    //获取socket输入流，接收数据
                    dataInputStream = new DataInputStream(socket.getInputStream());
                    //循环
                    while(true){
                        //接收数据
                        String revStr = dataInputStream.readUTF();
                        //打印信息
                        Date date = new Date(System.currentTimeMillis());
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                        System.out.println(df.format(date)+":"+revStr);
                        //如果服务端返回bye则退出
                        if(revStr.equals("bye")){
                            break;
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {

                }
            }
        }.start();  //启动线程

        //获取输出流
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

        while(true){

            //获取控制台数据
            String sendStr = scanner.nextLine();
            //输出数据
            dataOutputStream.writeUTF(sendStr);
            //舒心流，确保能输出
            dataOutputStream.flush();
        }
    }

}

class EchoServer {
    // TODO
    private Integer port;

    public EchoServer(Integer port) {
        this.port = port;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public void startService() throws IOException {
        //监听端口
        //因为telnet默认端口为23，此处使用23端口
        ServerSocket serverSocket = new ServerSocket(port);
        while(true){
            //等待客户端连接
            Socket socket = serverSocket.accept();
            //每一个客户端来专门启动线程处理
            new Thread(){
                @Override
                public void run() {
                    DataInputStream dataInputStream= null;
                    DataOutputStream dataOutputStream = null;

                    try {
                        //获取socket输入流
                        dataInputStream = new DataInputStream(socket.getInputStream());
                        //获取Socket输出流
                        dataOutputStream = new DataOutputStream(socket.getOutputStream());
                        //打印欢迎信息
                        dataOutputStream.writeUTF("欢迎使用echo服务器...");
                        dataOutputStream.flush();
                        while(true){
                            String revStr = dataInputStream.readUTF();
                            //把接收到的数据返回回去
                            dataOutputStream.writeUTF(revStr);
                            //刷新流，确保数据发送出去
                            dataOutputStream.flush();
                            if(revStr.equals("byte")){
                                break;
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            dataInputStream.close();
                            dataOutputStream.close();
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    }

}



